<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='list'){
	$params = array('uniacid'=>$_SESSION['uniacid']);
	$groups=pdo_getall('jy_farmer_lclassify',$params,'','','paixu asc');
	foreach($groups as &$item){
		$sql1 = "SELECT SUM(area) FROM " . tablename('jy_farmer_land') . " WHERE uniacid=:uniacid AND classify_id=:classify_id AND status=1";
		$area = pdo_fetch($sql1,array(":uniacid" => $_SESSION['uniacid'],":classify_id" => $item['id']));
		$item['area'] =$area['SUM(area)'];
		$sql2 = "SELECT SUM(area) FROM " . tablename('jy_farmer_land') . " WHERE uniacid=:uniacid AND classify_id=:classify_id AND issell=1";
		$sell_area = pdo_fetch($sql2,array(":uniacid" => $_SESSION['uniacid'],":classify_id" => $item['id']));
		$item['sell_area'] = $sell_area['SUM(area)'];
	}
	$id = intval($_RQ['id']);
	if(!$id){
		$land=pdo_getall('jy_farmer_lclassify',array('uniacid'=>$_SESSION['uniacid']));
		$id=$land[0]['id'];
	}
	$return['list']=array();
	$return['params']['id']=$id;
	$return['post']=array();
	$return['groups']=$groups;
	$sharelink='jy_farmer/lclassify//fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>'作物列表',
			'desc'=>$return['setting']['share_desc']?$return['setting']['share_desc']:'作物列表-天天农场，一个属于自己的农场！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($return['setting']['share_image'])
	);
	set_html('title','作物列表');
	exi($return);
}
if($_RQ['op']=='land_list'){
	$id=$_RQ['id'];
	//查询地块内土地
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=$params=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'issell'=>0);
	if($id){
		$params['classify_id']=$id;
	}
	$fields=array('id','image','title','status','issell','area','unarea','classify_id','remark');
	$land_list=pdo_getslice('jy_farmer_land',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$lclassify=pdo_get('jy_farmer_lclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	foreach($land_list as &$item){
		$item['price']=$lclassify['price'];
		$item['isarea']=pdo_getsum('jy_farmer_order',array('uniacid'=>$_SESSION['uniacid'],'land_id'=>$item['id'],'mid'=>$_SESSION['uid'],'status'=>array(1,3,4)),'num');
		$item['lastarea']=$item['area']-$item['lastarea'];
	}
	$return['list']=$land_list;
	exi($return);
}

if($_RQ['op']=='detail'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('没有找到该土地，请重新选择！','error','jy_farmer/lclassify/list');
	}
	//查询土地
	$land=pdo_get('jy_farmer_land',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$lclassify=pdo_get('jy_farmer_lclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$land['classify_id']));
	$land['price']=$lclassify['price'];
	$land['onarea']=$land['area'] - $land['unarea'];
	$seeds= str_array($land['seed_id']);
	if($seeds){
		$land['seeds']=pdo_getall('jy_farmer_seed',array('uniacid'=>$_SESSION['uniacid'],'id'=>$seeds));
	}
	$return['item']=$land;
	$return['post']=array();
	$sharelink='jy_farmer/lclassify/detail/id='.$land['id'].'&fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$land['title'],
			'desc'=>$land['remark']?:'作物详情-天天农场，一个属于自己的农场！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($land['image'])
	);
	set_html('title',$land['title']);
	exi($return);
}